package com.sunflower.bath.common.interf.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.framework.mybatis.Criteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sunflower.framework.utils.StringUtils;

import com.sunflower.bath.common.contants.BathContants;
import com.sunflower.bath.common.interf.IJob;
import com.sunflower.bath.common.modle.ResultMsg;
import com.sunflower.bath.model.JobNeedExecut;
import com.sunflower.bath.model.JobParams;

public class JarJob implements IJob {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Override
	public ResultMsg executJob(JobNeedExecut job) {
		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setIsSuccess(false);
		if(job==null||BathContants.JOB_TYPE_1!=job.getJobType()){
			logger.error("任务类型不匹配");
			resultMsg.setIsSuccess(false);
			resultMsg.setErrorCode("");
			resultMsg.setErrorMsg("任务类型不匹配");
			return resultMsg;
		}
		if(StringUtils.isNullOrBlank(job.getJobClass())){
			logger.error("任务{}，没有找到可执行的任务类",job.getJobName());
			resultMsg.setIsSuccess(false);
			resultMsg.setErrorCode("");
			resultMsg.setErrorMsg("数据库链接参数有误");
			return resultMsg;
		}
		if(StringUtils.isNullOrBlank(job.getJobMethod())){
			logger.error("任务{}，没有找到任务类({})可执行方法",job.getJobName(),job.getJobClass());
			resultMsg.setIsSuccess(false);
			resultMsg.setErrorCode("");
			resultMsg.setErrorMsg(String.format("任务%s，没有找到任务类(%s)可执行方法",job.getJobName(),job.getJobClass()));
			return resultMsg;
		}
		try {
			Class<?> jobClass =Class.forName(job.getJobClass());
			//获取所有的属性
			List<JobParams> jobParamsList = job.getJobParamsList();
			//按paramOrder排序
			Collections.sort(jobParamsList, new Comparator<JobParams>() {
				public int compare(JobParams o1, JobParams o2) {
					int order1 = (Integer) o1.getParamOrder();
					int order2 = (Integer) o2.getParamOrder();
					return order1 - order2;
				}
			});
			Class<?>[] paramClass = new Class[jobParamsList.size()];
			Object[] paramArry = new Object[jobParamsList.size()];
			for(int i=0;i<jobParamsList.size();i++){
				if(jobParamsList.get(i).getParamType()==0){
					paramClass[i]=String.class;
					paramArry[i]=jobParamsList.get(i).getStrVal();
				}
				else if(jobParamsList.get(i).getParamType()==1){
					paramClass[i]=Integer.class;
					paramArry[i]=jobParamsList.get(i).getIntVal();
				}
				else if(jobParamsList.get(i).getParamType()==2){
					paramClass[i]=Date.class;
					paramArry[i]=jobParamsList.get(i).getTimeVal();
				}
			}

			Method jobMethod = jobClass.getDeclaredMethod(job.getJobMethod(),paramClass);
			Class<?>[] parameterTypes = jobMethod.getParameterTypes();
			if(jobParamsList.size()!=parameterTypes.length){
				logger.error("任务类({})，设置参数个数与方法参数个数不一致",job.getJobName());
				resultMsg.setIsSuccess(false);
				resultMsg.setErrorCode("");
				resultMsg.setErrorMsg(String.format("任务类(%s)，设置参数个数与方法参数个数不一致",job.getJobName()));
				return resultMsg;
			}
			Object jobObject = jobClass.newInstance();
			jobMethod.setAccessible(true);//打破封装性
			jobMethod.invoke(jobObject,paramArry);
		} 
		catch (ClassNotFoundException e) 	{e.printStackTrace();} 
		catch (InstantiationException e) 	{e.printStackTrace();} 
		catch (IllegalAccessException e) 	{e.printStackTrace();} 
		catch (NoSuchMethodException e) 	{e.printStackTrace();} 
		catch (SecurityException e) 		{e.printStackTrace();}
		catch (IllegalArgumentException e) 	{e.printStackTrace();} 
		catch (InvocationTargetException e) {e.printStackTrace();}  
		return resultMsg;
	}
	
	@Override
	public ResultMsg addJob(JobNeedExecut job) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultMsg updateJob(JobNeedExecut job) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultMsg deleteJob(JobNeedExecut job) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultMsg selectJob(Criteria<JobNeedExecut> params) {
		// TODO Auto-generated method stub
		return null;
	}
	

	public static void main(String[] args) {

		JobNeedExecut job = new JobNeedExecut();
		job.setJobClass("com.sunflower.bath.common.interf.impl.Test");
		job.setJobMethod("jobTest");
		job.setJobType(BathContants.JOB_TYPE_1);
		List<JobParams> jobParamsList = new ArrayList<JobParams>();
		JobParams jobParams1 = new JobParams();
		jobParams1.setParamOrder(0);
		jobParams1.setParamType(0);
		jobParams1.setStrVal("ye");

		JobParams jobParams2 = new JobParams();
		jobParams2.setParamOrder(1);
		jobParams2.setParamType(1);
		jobParams2.setIntVal(28);

		JobParams jobParams3 = new JobParams();
		jobParams3.setParamOrder(2);
		jobParams3.setParamType(0);
		jobParams3.setStrVal("tongxue");

		jobParamsList.add(jobParams1);
		jobParamsList.add(jobParams3);
		jobParamsList.add(jobParams2);

		job.setJobParamsList(jobParamsList);

		new JarJob().executJob(job);

	}
}
